package com.luxoft.scoremanager.server.dao.listimpl;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.luxoft.scoremanager.server.dao.StaticDataDao;
import com.luxoft.scoremanager.server.model.dao.StaticDataDaoModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;



public class ListStaticDataDao implements StaticDataDao {
    private List<StaticDataDaoModel> staticDatatList = new ArrayList<StaticDataDaoModel>();

    public ListStaticDataDao() {
        init();
    }

    private void init() {
        staticDatatList.add(new StaticDataDaoModel(1, "Very weak", "1"));
        staticDatatList.add(new StaticDataDaoModel(2, "Weak", "1"));
        staticDatatList.add(new StaticDataDaoModel(3, "Adequate", "1"));
        staticDatatList.add(new StaticDataDaoModel(4, "Suspiciousk", "1"));
        staticDatatList.add(new StaticDataDaoModel(5, "Moderate", "1"));
        staticDatatList.add(new StaticDataDaoModel(6, "Very weak", "2"));
        staticDatatList.add(new StaticDataDaoModel(7, "Weak", "2"));
        staticDatatList.add(new StaticDataDaoModel(8, "Adequate", "2"));
    }

    @Override
    public void addStaticData(StaticDataDaoModel staticData) {
        checkNotNull(staticData);
        if (staticData.getId() == null) {
            staticData.setId((int) new Date().getTime());
        }
        staticDatatList.add(staticData);
    }

    @Override
    public void updateStaticData(StaticDataDaoModel staticData) {
        checkNotNull(staticData);
        if (staticData.getId() == null) {
            staticData.setId((int) new Date().getTime());
            staticDatatList.add(staticData);
        } else {
            staticDatatList.remove(staticData);
            staticDatatList.add(staticData);
        }
    }

    @Override
    public void deleteStaticData(StaticDataDaoModel staticData) {
        checkNotNull(staticData);
        checkNotNull(staticData.getId());
        staticDatatList.remove(staticData);
    }

    @Override
    public List<StaticDataDaoModel> getAll() {
        return staticDatatList;
    }

    @Override
    public List<StaticDataDaoModel> getAllBySet(final String set) {
        checkNotNull(set);
        Collection<StaticDataDaoModel> filteredList = Collections2.filter(staticDatatList, new Predicate<StaticDataDaoModel>() {
            @Override
            public boolean apply(StaticDataDaoModel input) {
                return (set.equals(input.getStaticDataSet()));
            }
        });
        return new ArrayList(filteredList);
    }

    @Override
    public StaticDataDaoModel getById(int id) {
        checkNotNull(id);
        for (StaticDataDaoModel staticData : staticDatatList) {
            if (id == staticData.getId()) {
                return staticData;
            }
            ;
        }
        return null;
    }
}
